#include "user_get.h"

int get_new(char *buf)
{
    struct device *p = head; // 遍历链表
    while (p != NULL)
    {
        if (strstr(p->device_name, buf)) // 找到设备
        {
            commodity_list_p q = commodity_head->next; // 遍历商品链表
            while (q != NULL)
            {

                char buf_t[1024] = {0};
                sprintf(buf_t, "%s,%s,%d\n", q->commodity->commodity_name, q->commodity->commodity_id, q->commodity_num);
                printf("%s,%s,%d\n", q->commodity->commodity_name, q->commodity->commodity_id, q->commodity_num);
                write(p->socket, buf_t, strlen(buf_t));
                q = q->next;
            }
            sleep(1);
            write(p->socket, "end", strlen("end"));
            break;
        }
        p = p->next;
    }
    return 0;
}

int take_off(char *buf)
{
    char number[1024] = {0};
    char *p = strtok(buf, ",");
    int i = 0;
    while (p != NULL)
    {
        printf("%s\n", p);
        if (i == 0)
            ;

        else if (i == 2)
        {
            strcpy(number, p);
        }
        p = strtok(NULL, ",");
        i++;
    }

    commodity_list_p q = commodity_head->next; // 遍历商品链表
    commodity_list_p prev = commodity_head;    // 前一个节点，初始为头节点
    while (q != NULL)
    {

        if (strcmp(q->commodity->commodity_id, number) == 0)
        {
            prev->next = q->next;
            free(q->commodity);
            free(q);
            printf("删除成功\n");
            break;
        }
        prev = q;
        q = q->next;
    }
return 0;
}


int take_off_sand(char *buf)
{
    
    struct device *p = head; // 遍历链表
    while (p != NULL)
    {
        if (strstr(p->device_name, buf)) // 找到设备
        {
            write(p->socket, "take_off", strlen("take_off"));
            sleep(1);
            commodity_list_p q = commodity_head->next; // 遍历商品链表
            while (q != NULL)
            {
                char buf_t[1024] = {0};
                sprintf(buf_t, "%s,%s,%s,%s", q->commodity->commodity_name, q->commodity->commodity_id, q->commodity->commodity_price,q->commodity->commodity_type);
                printf("%s,%s,%s,%s\n",q->commodity->commodity_name, q->commodity->commodity_id, q->commodity->commodity_price,q->commodity->commodity_type);
                write(p->socket, buf_t, strlen(buf_t));
                q = q->next;
            }
            sleep(1);
            write(p->socket, "end", strlen("end"));
            printf("转发成功\n");
            break;
        }
        p = p->next;
    }
    return 0;
}

int get_comm(char *buf)
{
    struct device *p = head; // 遍历链表
    while (p != NULL)
    {
        if (strstr(p->device_name, buf)) // 找到设备
        {
            FILE *fp = fopen("/root/kkcode/second_item/Data/forever_commodity","r+");
            if(fp==NULL)
            {
                printf("打开失败\n");
                return 0;
            }
            char buf_t[1024] = {0};
            while(fgets(buf_t,1024,fp)!=NULL)
            {
                write(p->socket, buf_t, strlen(buf_t));
            }
            sleep(1);
            write(p->socket, "end", strlen("end"));
            fclose(fp);
            break;
            
        }
        p = p->next;
    }
    return 0;
}